<template>
  <div>
    <div class="left-title flex" style="display: flex; justify-content: space-between;margin-top: 2rem;">

      <div class="title">
        投料分组划分
      </div>
      <div v-allow="'030101'" class="add-btn" @click="handleAdd()">
        添加模板
      </div>
    </div>
    <NxTable :isIndex="false" :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap"
      @refresh="refresh">
      <template slot="taskTime" slot-scope="scope">
        <span>{{ scope.row.taskTime }}(分)</span>
      </template>
      <template slot="type" slot-scope="scope">
        <span v-if="cope.row.type == 0">排污换水区域</span>
        <span v-if="cope.row.type == 1">增氧区域</span>
        <span v-if="cope.row.type == 2">投料分组</span>
      </template>
      <template slot="operation" slot-scope="scope">
        <span v-allow="'030105'" @click="handleDetail(scope.row)" class="nx-btn nx-edit">投料配置</span>
        <span v-allow="'030103'" @click="handleEdit(scope.row)" class="nx-btn nx-edit">编辑</span>
        <span v-allow="'030104'" @click="handleDelete(scope.row)" class="nx-btn nx-del">删除</span>
      </template>
    </NxTable>
    <AddEditDialog :type="type" :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog">
    </AddEditDialog>
    <addBatchCharge ref="add"></addBatchCharge>
  </div>
</template>
<script>
import NxTable from '@/components/NxTablePc.vue'
import AddEditDialog from './addEditDialog.vue'
import addBatchCharge from './addBatchCharge.vue'
import { Page } from '@/utils/pagination'
import {
  delConfigAutoTaskGroup,
  saveConfigAutoTaskGroup,
  getConfigAutoTaskGroupList,
} from '@/api/system'
import { getCompanyInfoTree } from '@/api/company'
import mixin from '@/api/alarmgetCompany.js'
export default {
  mixins: [mixin],
  components: {
    NxTable,
    AddEditDialog,
    addBatchCharge
  },
  // 表格数据
  data() {
    return {
      activeName: 'first',
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        baseId: '',
        type: 2,
      },
      type: 2,
      // 表格字段
      tableCols: [
        {
          label: '分组名称',
          prop: 'name',
          align: 'center',
          width: 120
        },
        {
          label: '区域',
          prop: 'names',
          align: 'center',
        },
        {
          label: '执行顺序',
          prop: 'orderNum',
          align: 'center',
        },
        {
          label: '任务时长',
          slot: 'taskTime',
          align: 'center',
        },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 180,
          fixed: 'right',
        },
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      baseArray: [], // 角色集合
      companyArray: [], // 公司集合
      userType: '', // 用户类型（0超级管理员，1公司管理员，2公司员工）
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.searchForm.userType = this.userType
    this.getCompanyInfoTree()
    setTimeout(() => {
      this.getList()
    }, 1000);
  },
  // 自定义方法
  methods: {

    handleDetail(row) {
      this.$refs.add.addEditIsDialog_c = true
      this.$refs.add.ids = row.ids
      this.$refs.add.names = row.names
      this.$refs.add.id = row.id
      this.$refs.add.getids(row.id)

    },
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()

    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        baseId: '',
        code: '',
        companyId: '',
        deviceStatus: '',
        name: '',
        tailWaterModel: '',
        type: '',
      }

      this.onSubmit()
    },
    // 查询列表
    getList() {
      let pollution = this.searchForm
      pollution.type = 2

      getConfigAutoTaskGroupList(pollution).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.pagination.count = res.data.total
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delConfigAutoTaskGroup({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg,
              })
              this.getList()
            }
          })
        })
        .catch((err) => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
    },

    // 用户类型切换
    typeChange() {
      this.searchForm.companyId = ''
      this.companyArray = []

      this.searchForm.roleId = ''
      this.baseArray = []

      // 获取公司列表
      this.getCompanyInfoTree()

      // 查询
      this.onSubmit()
    },

    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }
      this.searchForm.baseId = ''
      this.baseArray = []
      this.getList()
      // 获取角色列表
      this.getSysCompanyBases()
    },
  },
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}

.title {
  font-size: 20px;
  font-weight: bold;
  color: #166bb6;
  text-shadow: 2px 3px 1px rgb(226, 226, 226);
  margin-left: 1rem;
}
</style>
